using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using MultiAgentsServer.Scripts.AgentTemplates;
using MultiAgentsServer.Scripts.Database;
using MultiAgentsServer.Scripts.Shared;
using System.Text.Json;
using static MultiAgentsServer.Scripts.Shared.ModelConsts;

namespace MultiAgentsServer.Pages;

[Authorize]
public class ModelConfigurationModel : PageModel
{
    // Here you can inject any services you need, such as a database service
    private readonly DataBaseOperationService _dbOperationService;
    public List<string> GptModelList { get; private set; }

    public ModelConfigurationModel(DataBaseOperationService dbOperationService)
    {
        _dbOperationService = dbOperationService;
        GptModelList = ModelNames;
    }

    // Properties to hold the selected values
    [BindProperty] public string LLMSwitchNodeModel { get; set; }
    [BindProperty] public string ParameterExtractionNodeModel { get; set; }
    [BindProperty] public string RenameChatRoomModel { get; set; }
    [BindProperty] public string ReplyAssistantModel { get; set; }
    [BindProperty] public string DefaultAgentModel { get; set; }
    [BindProperty] public string GitOperatorAgentModel { get; set; }
    [BindProperty] public string OctoHelperAgentModel { get; set; }

    public void OnGet()
    {
        string jsonConfigData = LoadConfiguration();
        if (!string.IsNullOrEmpty(jsonConfigData))
        {
            using (JsonDocument doc = JsonDocument.Parse(jsonConfigData))
            {
                JsonElement root = doc.RootElement;

                LLMSwitchNodeModel = root.GetProperty("LLMSwitchNodeModel").GetString();
                ParameterExtractionNodeModel = root.GetProperty("ParameterExtractionNodeModel").GetString();
                RenameChatRoomModel = root.GetProperty("RenameChatRoomModel").GetString();
                ReplyAssistantModel = root.GetProperty("ReplyAssistantModel").GetString();
                DefaultAgentModel = root.GetProperty("DefaultAgentModel").GetString();
                GitOperatorAgentModel = root.GetProperty("GitOperatorAgentModel").GetString();
                OctoHelperAgentModel = root.GetProperty("OctoHelperAgentModel").GetString();
            }
        }
    }


    public IActionResult OnPost()
    {
        var configData = new
        {
            LLMSwitchNodeModel,
            ParameterExtractionNodeModel,
            ReplyAssistantModel,
            RenameChatRoomModel,
            DefaultAgentModel,
            GitOperatorAgentModel,
            OctoHelperAgentModel
        };

        string jsonConfigData = JsonSerializer.Serialize(configData);
        StoreConfiguration(jsonConfigData);

        // Redirect to the same or another page indicating success or refresh the state

        // Handle the values posted from the form
        // For example, save them to the database
        return RedirectToPage();
    }

    public static void StoreConfiguration(string configurationData)
    {
        string documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
#if LOCALDEV
        string configFileName = Path.Combine(documentsDirectory, @"MultiAgents\default_model_management_localdev.json");
#elif DEVELOP
        string configFileName = Path.Combine(documentsDirectory, @"MultiAgents\default_model_management_develop.json");
#elif RELEASE
        string configFileName = Path.Combine(documentsDirectory, @"MultiAgents\default_model_management_release.json");
#endif


        // Check if file exists and create if not
        System.IO.File.WriteAllText(configFileName, configurationData);
    }
    public static string LoadConfiguration()
    {
        string documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
#if LOCALDEV
        string configFileName = Path.Combine(documentsDirectory, @"MultiAgents\default_model_management_localdev.json");
#elif DEVELOP
        string configFileName = Path.Combine(documentsDirectory, @"MultiAgents\default_model_management_develop.json");
#elif RELEASE
        string configFileName = Path.Combine(documentsDirectory, @"MultiAgents\default_model_management_release.json");
#endif

        if (System.IO.File.Exists(configFileName))
        {
            return System.IO.File.ReadAllText(configFileName);
        }

        return null;
    }
}
